/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.ui.edit;

import com.daikit.daikit4gxt.client.ui.edit.ReferenceSelectorEvent.ReferenceSelectorHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;

/**
 * Event for reference selector selection
 * 
 * @author Thibaut CASELLI
 * 
 * @param <M>
 *            the type of the selected object
 */
@SuppressWarnings("rawtypes")
public class ReferenceSelectorEvent<M> extends GwtEvent<ReferenceSelectorHandler>
{
	/**
	 * Handler type.
	 */
	private static Type<ReferenceSelectorHandler> TYPE;

	private M selectedObject;

	/**
	 * Gets the type associated with this event.
	 * 
	 * @return returns the handler type
	 */
	public static Type<ReferenceSelectorHandler> getType()
	{
		if (TYPE == null)
		{
			TYPE = new Type<ReferenceSelectorHandler>();
		}
		return TYPE;
	}

	/**
	 * Constructor
	 * 
	 * @param selectedObject
	 *            the edited object
	 */
	public ReferenceSelectorEvent(final M selectedObject)
	{
		this.selectedObject = selectedObject;
	}

	@Override
	public Type<ReferenceSelectorHandler> getAssociatedType()
	{
		return TYPE;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void dispatch(final ReferenceSelectorHandler handler)
	{
		handler.onReferenceSelected(this);
	}

	/**
	 * @return the selectedObject
	 */
	public M getSelectedObject()
	{
		return selectedObject;
	}

	/**
	 * @param selectedObject
	 *            the selectedObject to set
	 */
	public void setSelectedObject(final M selectedObject)
	{
		this.selectedObject = selectedObject;
	}

	/**
	 * Handler for {@link ReferenceSelectorEvent} events.
	 * 
	 * @param <M>
	 *            the type of the object to be edited
	 */
	public interface ReferenceSelectorHandler<M> extends EventHandler
	{
		/**
		 * Called after a Edit button is selected.
		 * 
		 * @param event
		 *            the {@link SelectEvent} that was fired
		 */
		void onReferenceSelected(ReferenceSelectorEvent<M> event);
	}

	/**
	 * A widget that implements this interface is a public source of {@link SelectEvent} events.
	 * 
	 * @param <M>
	 *            the type of the object to be edited
	 */
	public interface HasReferenceSelectorHandlers<M>
	{

		/**
		 * Edits a {@link SelectHandler} handler for {@link ReferenceSelectorEvent} events.
		 * 
		 * @param handler
		 *            the handler
		 * @return the registration for the event
		 */
		HandlerRegistration addReferenceSelectorHandler(ReferenceSelectorHandler<M> handler);
	}
}
